package org.jypj.dev.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.jypj.dev.model.TrainNumInfo;
import org.jypj.dev.dao.TrainNumInfoDao;
import org.jypj.dev.service.TrainNumInfoService;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;

@Service("trainNumInfoService")
public class TrainNumInfoServiceImpl implements TrainNumInfoService {

    @Resource
    private TrainNumInfoDao trainNumInfoDao;

    /**
     * 保存
     * 所有字段全都保存
     *
     * @param trainNumInfo
     * @return 保存后的对象包括ID
     */
    public int saveTrainNumInfo(TrainNumInfo trainNumInfo) {
        if (StringUtil.isEmpty(trainNumInfo.getId())) {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            trainNumInfo.setId(uuid);
        }
        return trainNumInfoDao.saveTrainNumInfo(trainNumInfo);
    }

    @Override
    public int saveOrUpdateTrainNumInfos(List<TrainNumInfo> trainNumInfos) {
        for (TrainNumInfo trainNumInfo : trainNumInfos) {
            TrainNumInfo trainNumInfo1 = trainNumInfoDao.selectTrainNumInfoById(trainNumInfo.getId());
            if (trainNumInfo1 != null) {
                trainNumInfoDao.updateTrainNumInfo(trainNumInfo);
            } else {
                trainNumInfoDao.saveTrainNumInfo(trainNumInfo);
            }
        }
        return 0;
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteTrainNumInfoById(String id) {

        return trainNumInfoDao.deleteTrainNumInfoById(id);
    }

    /**
     * 根据对象删除
     *
     * @param trainNumInfo
     * @return
     */
    public int deleteTrainNumInfoByObject(TrainNumInfo trainNumInfo) {

        return trainNumInfoDao.deleteTrainNumInfoByObject(trainNumInfo);
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param trainNumInfo
     * @return 保存后的对象包括ID
     */
    public int updateTrainNumInfo(TrainNumInfo trainNumInfo) {

        return trainNumInfoDao.updateTrainNumInfo(trainNumInfo);
    }

    /**
     * 按ID查询
     *
     * @return TrainNumInfo
     * @parm id 主键ID
     */
    public TrainNumInfo selectTrainNumInfoById(String id) {

        return trainNumInfoDao.selectTrainNumInfoById(id);
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<TrainNumInfo>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<TrainNumInfo> trainNumInfos = trainNumInfoDao.selectOnePageByMap(page, map);
        if (trainNumInfos != null && trainNumInfos.size() > 0) {
            page.setResult(trainNumInfos);
        } else {
            page.setResult(new ArrayList<TrainNumInfo>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page         分页对象
     * @param trainNumInfo 查询条件
     * @return Page
     */
    public Page selectOnePageByTrainNumInfo(Page page, TrainNumInfo trainNumInfo) {
        List<TrainNumInfo> trainNumInfos = trainNumInfoDao.selectOnePageByTrainNumInfo(page, trainNumInfo);
        if (trainNumInfos != null && trainNumInfos.size() > 0) {
            page.setResult(trainNumInfos);
        } else {
            page.setResult(new ArrayList<TrainNumInfo>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<TrainNumInfo>
     */
    public List<TrainNumInfo> selectAllByMap(Map<String, Object> map) {
        return trainNumInfoDao.selectAllByMap(map);
    }


    /**
     * 按条件查询全部的
     *
     * @param trainNumInfo 查询条件
     * @return List<TrainNumInfo>
     */
    public List<TrainNumInfo> selectAllByTrainNumInfo(TrainNumInfo trainNumInfo) {

        return trainNumInfoDao.selectAllByTrainNumInfo(trainNumInfo);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return TrainNumInfo
     */
    public TrainNumInfo selectObjectByMap(Map<String, Object> map) {

        return trainNumInfoDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @param trainNumInfo 查询条件
     * @return TrainNumInfo
     */
    public TrainNumInfo selectObjectByTrainNumInfo(TrainNumInfo trainNumInfo) {

        return trainNumInfoDao.selectObjectByTrainNumInfo(trainNumInfo);
    }
}